代码改变世界

Java笔记1(位运算)

2015-08-10 17:05  马尔代夫_珍  阅读(261)  评论(0编辑  收藏  举报

1、位运算:| 操作符使用
public class WeiHuoTest{
       public static void main(String[] args){
               int number1 = 10;
              int number2 = 8;
  /*
    number1: 10
    0000 0000 0000 0000 0000 0000 0000 1010
    number2: 8
    0000 0000 0000 0000 0000 0000 0000 1000
  -----------------------------------------
  0000 0000 0000 0000 0000 0000 0000 1010
  0000 0000 0000 0000 0000 0000 0000 1000
  ---------------------------------------
  0000 0000 0000 0000 0000 0000 0000 1010 --->10
   
  */
  System.out.println(number1|number2);
 }
}

2、位运算符:& 的操作 

public class WeiYuTest{  

      public static void main(String[] args){   

                   int number1 = 10;  

                   int number2 = 8;

        /*     number1: 10   

                             0000 0000 0000 0000 0000 0000 0000 1010    

                number2: 8    

                             0000 0000 0000 0000 0000 0000 0000 1000   

                   ------------------------------------------  

                             0000 0000 0000 0000 0000 0000 0000 1010   

                             0000 0000 0000 0000 0000 0000 0000 1000   

                     ---------------------------------------  

                             0000 0000 0000 0000 0000 0000 0000 1000----->8

  */

  System.out.println(number1&number2);  } }

3、位运算符:^ 异或操作符的使用

 public class WeiYiHuoTest{

            public static void main(String[] args){  

                            int number1 = 10;   int number2 = 8;

  /*     number1: 10    

                  0000 0000 0000 0000 0000 0000 0000 1010   

           number2: 8    

                  0000 0000 0000 0000 0000 0000 0000 1000  

                               -----------------------------------------   

                  0000 0000 0000 0000 0000 0000 0000 1010    

                  0000 0000 0000 0000 0000 0000 0000 1000    

                                 ---------------------------------------    

                  0000 0000 0000 0000 0000 0000 0000 0010---->2   */

  System.out.println(number1^number2);  } }

4、位运算符:>>、<<操作符的使用

 public class WeiYiDong{

              public static void main(String[] args){   

                               int number = 8;

  /* 右移  

                    0000 0000 0000 0000 0000 0000 0000 1000   

                    0000 0000 0000 0000 0000 0000 0000 0010    ---->2   */

                  System.out.println(number>>2);   

 /* 左移   

                    0000 0000 0000 0000 0000 0000 0000 1000  

                    0000 0000 0000 0000 0000 0000 0010 0000    ---->32   */              System.out.println(number<<2);

                             int number1 = -8;   

/* 右移  

                     1000 0000 0000 0000 0000 0000 0000 1000  

                     1000 0000 0000 0000 0000 0000 0000 0010      -2    */  

               System.out.println(number1>>2);    } }

5、位运算:>>>无符号的右移

 public class WeiWfh{  

        public static void main(String[] args){   

                                int number = -10;

  /*  

      1000 0000 0000 0000 0000 0000 0000 1010(原码)   

      1111 1111 1111 1111 1111 1111 1111 0101(反码)  

 +                                                                  1  

           ------------------------------------------  

       1111 1111 1111 1111 1111 1111 1111 0110 (补码)  

       0011 1111 1111 1111 1111 1111 1111 1101  移位后的(正数)

            ----------------------------------------   

        0011 1111 1111 1111 1111 1111 1111 1101(正数的原码)

     */

  System.out.println(number>>>2);  } }